package com.grandbroad.eim.adapter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.west.westclient.api.hotvod.bean.VodList;
import com.easy.west.westclient.api.vod.bean.ClassBean;
import com.google.gson.Gson;
import com.grandbroad.eim.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class VodViewPagerAdapter extends PagerAdapter {
    private AppCompatActivity activity;
    List<ClassBean> classBeans = new ArrayList<>();
    private String url = "https://api.gaobohuike.com/demo-0.0.1-SNAPSHOT/classifies/findById/";
    public void setClassBeans(List<ClassBean> classBeans) {
        this.classBeans = classBeans;
    }

    public VodViewPagerAdapter(AppCompatActivity activity)
    {
       this.activity = activity;
    }

    @Override
    public int getCount() {
        return classBeans.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return classBeans.get(position).getName();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.recycler_view,container,false);
        final RecyclerView re = view.findViewById(R.id.recycler_vod);
        final VodListAdpter listAdpter = new VodListAdpter(activity);
        re.setAdapter(listAdpter);
        re.setLayoutManager(new LinearLayoutManager(container.getContext()));
        x.http().get(new RequestParams(url + classBeans.get(position).getCid()), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                VodList list = gson.fromJson(result,VodList.class);
                if (list.getStatus()==1)
                {
                    listAdpter.setVodList(list.getData());
                    listAdpter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view ==(View) o;
    }
}
